package client;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.FilePath;

import Dao.FoodsDao;
import Dao.ItemDao;
import Dao.OrdersDao;
import Dao.WaitersDao;
import Dao.Impl.FoodsDaoImpl;
import Dao.Impl.ItemDaoImpl;
import Dao.Impl.OrdersDaoImpl;
import Dao.Impl.WaitersDaoImpl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import entity.FoodMenu;
import entity.Foods;
import entity.Item;
import entity.Orders;
import entity.Waiters;

public class OrderServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void allOrders(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setCharacterEncoding("utf-8");
			int user_no = Integer.valueOf(request.getParameter("user_no"));
			OrdersDao dao = new OrdersDaoImpl();
			List<Orders> order =  dao.queryByUserNo(user_no);
			Gson gson =new GsonBuilder()  
			  .setDateFormat("yyyy-MM-dd")  
			  .create();
			String ret = gson.toJson(order);
			PrintWriter pw =  response.getWriter();
			pw.print(ret);
	}

	public void assignTableAndWaiter(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
			int order_no = Integer.valueOf(request.getParameter("order_no"));
			int waiter_no = Integer.valueOf(request.getParameter("waiter_no"));
			WaitersDao dao = new WaitersDaoImpl();
			PrintWriter pw = response.getWriter();
			try {
				Waiters w =  dao.get(waiter_no);
				if(w == null) {
					pw.print("error:the waiter is not exist");
					return;
				}
				OrdersDao orderDao = new OrdersDaoImpl();
				Orders order = orderDao.getByOrder_no(order_no);
				order.setWaiter_no(waiter_no);
				order.setTable_no(0);
				orderDao.update(order);
				pw.print("true");
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void checkWaiter (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		int waiter_no = Integer.valueOf(request.getParameter("waiter_no"));
		PrintWriter pw = response.getWriter();
		WaitersDao dao = new WaitersDaoImpl();
		try {
			Waiters w =  dao.get(waiter_no);
			if(w == null) {
				pw.print("false");
				return;
			}
			pw.print("true");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void queryFoodsList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		int order_no = Integer.valueOf(request.getParameter("order_no"));
		ItemDao itemDao = new ItemDaoImpl();
		List<FoodMenu> menus = new ArrayList<FoodMenu>();
		try {
			List<Item> items = itemDao.getList(order_no);
			FoodsDao foodDao = new FoodsDaoImpl();
			for (Item item : items) {
				FoodMenu menu = new FoodMenu();
				menu.setNumber(item.getMealNo());
				Foods food = foodDao.getFood(item.getFood_no());
				String url = FilePath.imageUrl(request, food.getFood_picAdd());
				food.setFood_picAdd(url);
				menu.setFood(food);
				menus.add(menu);
			}
			if(menus.size() > 0) {
				Gson gson = new Gson();
				String ret = gson.toJson(menus);
				response.getWriter().print(ret);
			} else {
				response.getWriter().print("error:order_no invalidate");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	

}
